﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DwProjectTemplate.DAL
{
	public class ExampleModule_Category : DwCustomDataRecord
	{
		private const string dbTableName = "Community_ExampleModule_Category";

		public string Name { get; set; }

		public ExampleModule_Category()
		{

		}

		public bool Save( Dynamicweb.Modules.UserManagement.User user )
		{
			bool rtnValue = false;
			try
			{
				using ( IDbConnection con = Dynamicweb.Database.GetConn( "DwProjectTemplate.mdb" ) )
				{
					if ( Dynamicweb.Database.IsAccess )
					{
						IDbCommand cmdCheck = con.CreateCommand();
						cmdCheck.CommandText = string.Format( "SELECT Id, LanguageId FROM {0} WHERE Id='{1}' AND LanguageId='{2}'", dbTableName, this.Id, this.LanguageId );
						IDataReader rsCheck = cmdCheck.ExecuteReader( CommandBehavior.SingleRow );

						bool update = true;
						if ( !rsCheck.Read() )
							update = false;
						rsCheck.Close();

						IDbCommand cmd = con.CreateCommand();
						if ( update )
						{
							cmd.CommandText = string.Format( "UPDATE [{0}] SET [Name]=?, [UpdatedDatetime]=?, [Active]=?, [UpdatedByUserId]=? WHERE [Id]=? AND [LanguageId]=?", dbTableName );
							IDbDataParameter p1 = cmd.CreateParameter();
							IDbDataParameter p2 = cmd.CreateParameter();
							IDbDataParameter p3 = cmd.CreateParameter();
							IDbDataParameter p4 = cmd.CreateParameter();
							IDbDataParameter p5 = cmd.CreateParameter();
							IDbDataParameter p6 = cmd.CreateParameter();

							p1.Value = Dynamicweb.Database.SqlString( this.Name );
							p2.Value = Dynamicweb.Database.SqlDate( DateTime.Now );
							p3.Value = Dynamicweb.Database.SqlBool( ( this.Active.HasValue ) ? this.Active.Value : false );
							p4.Value = Dynamicweb.Database.SqlString( user.ID.ToString() );
							p5.Value = Dynamicweb.Database.SqlString( this.Id );
							p6.Value = Dynamicweb.Database.SqlString( this.LanguageId );

							cmd.Parameters.Add( p1 );
							cmd.Parameters.Add( p2 );
							cmd.Parameters.Add( p3 );
							cmd.Parameters.Add( p4 );
							cmd.Parameters.Add( p5 );
							cmd.Parameters.Add( p6 );

						}
						else
						{
							cmd.CommandText = string.Format( "INSERT INTO [{0}] ([Id], [LanguageId], [Name], [CreatedDatetime], [UpdatedDatetime], [Active], [CreatedByUserId], [UpdatedByUserId]) " +
																"VALUES( ?, ?, ?, ?, ?, ?, ?, ? )", dbTableName );
							IDbDataParameter p1 = cmd.CreateParameter();
							IDbDataParameter p2 = cmd.CreateParameter();
							IDbDataParameter p3 = cmd.CreateParameter();
							IDbDataParameter p4 = cmd.CreateParameter();
							IDbDataParameter p5 = cmd.CreateParameter();
							IDbDataParameter p6 = cmd.CreateParameter();
							IDbDataParameter p7 = cmd.CreateParameter();
							IDbDataParameter p8 = cmd.CreateParameter();

							p1.Value = Dynamicweb.Database.SqlString( this.Id );
							p2.Value = Dynamicweb.Database.SqlString( this.LanguageId );
							p3.Value = Dynamicweb.Database.SqlString( this.Name );
							p4.Value = Dynamicweb.Database.SqlDate( DateTime.Now );
							p5.Value = Dynamicweb.Database.SqlDate( DateTime.Now );
							p6.Value = Dynamicweb.Database.SqlBool( ( this.Active.HasValue ) ? this.Active.Value : false );
							p7.Value = Dynamicweb.Database.SqlString( user.ID.ToString() );
							p8.Value = Dynamicweb.Database.SqlString( user.ID.ToString() );

							cmd.Parameters.Add( p1 );
							cmd.Parameters.Add( p2 );
							cmd.Parameters.Add( p3 );
							cmd.Parameters.Add( p4 );
							cmd.Parameters.Add( p5 );
							cmd.Parameters.Add( p6 );
							cmd.Parameters.Add( p7 );
							cmd.Parameters.Add( p8 );

						}
						cmd.ExecuteNonQuery();

					}
					else
					{
						using ( DB.dbDataContext db = new DB.dbDataContext( con ) )
						{
							DB.Community_ExampleModule_Category dbObject = ( from c in db.Community_ExampleModule_Categories
																															 where c.Id.Equals( this.Id ) && c.LanguageId.Equals( this.LanguageId )
																															 select c ).SingleOrDefault();

							if ( dbObject == null )
							{
								dbObject = new DB.Community_ExampleModule_Category();
								db.Community_ExampleModule_Categories.InsertOnSubmit( dbObject );
							}
							dbObject.Active = ( this.Active.HasValue ) ? this.Active.Value : false;
							dbObject.CreatedByUserId = this.CreatedByUserId;
							dbObject.CreatedDatetime = this.CreatedDateTime;
							dbObject.Id = this.Id;
							dbObject.LanguageId = this.LanguageId;
							dbObject.Name = this.Name;
							dbObject.UpdatedByUserId = this.UpdatedByUserId;
							dbObject.UpdatedDatetime = this.UpdatedDateTime;

							db.SubmitChanges();
						}
					}
				}
				rtnValue = true;
			}
			catch ( OleDbException e )
			{
				App.LogFactory.Log( e.ToString(), App.LogFactory.CustomLogFolder );
			}
			catch ( SqlException e )
			{
				App.LogFactory.Log( e.ToString(), App.LogFactory.CustomLogFolder );
			}
			catch ( Exception e )
			{
				App.LogFactory.Log( e.ToString(), App.LogFactory.CustomLogFolder );
			}
			finally
			{

			}
			return ( rtnValue );
		}
	}
}